package com.ksd.web.controller.service;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "asyncExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);          // 核心线程数
        executor.setMaxPoolSize(10);         // 最大线程数
        executor.setQueueCapacity(25);       // 队列容量
        executor.setThreadNamePrefix("async-asyncExecutor"); // 线程名前缀（方便日志追踪）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略：让提交任务的线程执行任务
        executor.setKeepAliveSeconds(30);    // 非核心线程空闲30秒后销毁
        // 移除手动initialize()，交给Spring自动初始化
        return executor;
    }

    @Bean(name = "asyncClearAgainScanExecutor")
    public Executor asyncClearAgainScanExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);          // 核心线程数
        executor.setMaxPoolSize(10);         // 最大线程数
        executor.setQueueCapacity(25);       // 队列容量
        executor.setThreadNamePrefix("async-asyncClearAgainScanExecutor"); // 线程名前缀（方便日志追踪）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略：让提交任务的线程执行任务
        executor.setKeepAliveSeconds(30);    // 非核心线程空闲30秒后销毁
        // 移除手动initialize()，交给Spring自动初始化
        return executor;
    }


    @Bean(name = "asyncZipDataExecutor")
    public Executor asyncZipDataExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);          // 核心线程数
        executor.setMaxPoolSize(10);         // 最大线程数
        executor.setQueueCapacity(25);       // 队列容量
        executor.setThreadNamePrefix("async-asyncZipDataExecutor"); // 线程名前缀（方便日志追踪）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略：让提交任务的线程执行任务
        executor.setKeepAliveSeconds(30);    // 非核心线程空闲30秒后销毁
        // 移除手动initialize()，交给Spring自动初始化
        return executor;
    }

    @Bean(name = "asyncNetworkConfig")
    public Executor asyncNetworkConfig() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);          // 核心线程数
        executor.setMaxPoolSize(10);         // 最大线程数
        executor.setQueueCapacity(25);       // 队列容量
        executor.setThreadNamePrefix("async-asyncNetworkConfig"); // 线程名前缀（方便日志追踪）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略：让提交任务的线程执行任务
        executor.setKeepAliveSeconds(30);    // 非核心线程空闲30秒后销毁
        // 移除手动initialize()，交给Spring自动初始化
        return executor;
    }
}
